package com.hgb.mall.portal.services.impl;

import com.hgb.mall.mbg.entity.UmsMember;
import com.hgb.mall.mbg.entity.UmsMemberReceiveAddress;
import com.hgb.mall.mbg.mapper.UmsMemberReceiveAddressDynamicSqlSupport;
import com.hgb.mall.mbg.mapper.UmsMemberReceiveAddressMapper;
import com.hgb.mall.portal.services.UmsMemberReceiveAddressService;
import com.hgb.mall.portal.services.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;

/**
 * 用户地址管理Service实现类
 * Created by hgb on 2020/8/28.
 */
@Service
public class UmsMemberReceiveAddressServiceImpl implements UmsMemberReceiveAddressService {
    @Autowired
    private UmsMemberService memberService;
    @Autowired
    private UmsMemberReceiveAddressMapper addressMapper;

    @Override
    public int add(UmsMemberReceiveAddress address) {
        UmsMember currentMember = memberService.getCurrentMember();
        if (address.getDefaultStatus() == 1) {
            //先将原来的默认地址去除
            addressMapper.update(c -> c
                    .set(UmsMemberReceiveAddressDynamicSqlSupport.defaultStatus).equalTo(0)
                    .where(UmsMemberReceiveAddressDynamicSqlSupport.memberId, isEqualTo(currentMember.getId()))
                    .and(UmsMemberReceiveAddressDynamicSqlSupport.defaultStatus, isEqualTo(1))
            );
        }
        address.setMemberId(currentMember.getId());
        return addressMapper.insert(address);
    }

    @Override
    public int delete(Long id) {
        UmsMember currentMember = memberService.getCurrentMember();
        return addressMapper.delete(c -> c
                .where(UmsMemberReceiveAddressDynamicSqlSupport.memberId, isEqualTo(currentMember.getId()))
                .and(UmsMemberReceiveAddressDynamicSqlSupport.id, isEqualTo(id))
        );
    }

    @Override
    public int update(Long id, UmsMemberReceiveAddress address) {
        address.setId(id);
        UmsMember currentMember = memberService.getCurrentMember();
        if (address.getDefaultStatus() == 1) {
            //先将原来的默认地址去除
            addressMapper.update(c -> c
                    .set(UmsMemberReceiveAddressDynamicSqlSupport.defaultStatus).equalTo(0)
                    .where(UmsMemberReceiveAddressDynamicSqlSupport.memberId, isEqualTo(currentMember.getId()))
                    .and(UmsMemberReceiveAddressDynamicSqlSupport.defaultStatus, isEqualTo(1))
            );
        }
        return addressMapper.updateByPrimaryKeySelective(address);
    }

    @Override
    public List<UmsMemberReceiveAddress> list() {
        UmsMember currentMember = memberService.getCurrentMember();
        return addressMapper.select(c -> c
                .where(UmsMemberReceiveAddressDynamicSqlSupport.memberId, isEqualTo(currentMember.getId()))
                .orderBy(UmsMemberReceiveAddressDynamicSqlSupport.defaultStatus.descending())
        );
    }

    @Override
    public UmsMemberReceiveAddress getItem(Long id) {
        UmsMember currentMember = memberService.getCurrentMember();
        List<UmsMemberReceiveAddress> addressList =
                addressMapper.select(c -> c
                        .where(UmsMemberReceiveAddressDynamicSqlSupport.memberId, isEqualTo(currentMember.getId()))
                        .and(UmsMemberReceiveAddressDynamicSqlSupport.id, isEqualTo(id))
                );
        if (!CollectionUtils.isEmpty(addressList)) {
            return addressList.get(0);
        }
        return null;
    }
}
